<template>
  <div>

    <el-button type="primary" @click="openDialog">添加题目</el-button>
    <el-dialog title="添加题目" :visible.sync="dialogVisible" width="50%">
      <el-form ref="questionForm" :model="gameOption" label-width="120px">
        <el-form-item label="题目内容">
          <el-input v-model="gameOption.gameOption.content"></el-input>
        </el-form-item>
        <el-form-item label="上传文件">
          <el-upload
              class="upload-demo"
              action="https://qianyan-1326849604.cos.ap-nanjing.myqcloud.com/audio/game"
              :file-list="fileList"
              :auto-upload="false"
          >
            <el-button type="primary" @click="uploadFile">上传文件</el-button>
            <el-button size="small" >确认上传</el-button>
          </el-upload>
        </el-form-item>
        <el-form-item label="选项">
          <el-button @click="addOption">添加选项</el-button>
          <el-button @click="removeOption">删除选项</el-button>
          <el-row v-for="(option, index) in gameOption.optionItems" :key="index">
            <el-col :span="12">
              <el-input v-model="option.content" placeholder="选项内容"></el-input>
            </el-col>
            <el-col :span="12">
              <el-upload
                  class="upload-demo"
                  action="https://qianyan-1326849604.cos.ap-nanjing.myqcloud.com/audio/game"
                  :file-list="fileList"
                  :auto-upload="false"
              >
                <el-button slot="trigger" size="small" type="primary">点击上传</el-button>
                <el-button size="small" type="success" @click="submitOptionUpload(index)">确认上传</el-button>
              </el-upload>
            </el-col>
          </el-row>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="add(gameOption)">确 定</el-button>
      </span>
    </el-dialog>


    <el-table
        :data="questions"
        style="width: 100%">
      <el-table-column
          prop="id"
          label="ID">
      </el-table-column>
      <el-table-column
          prop="content"
          label="标题">
      </el-table-column>
      <el-table-column
          prop="videoPath"
          label="题目录音文件位置">
      </el-table-column>

      <el-table-column
          prop="currentPath"
          label="正确答案">
      </el-table-column>
      <el-table-column
          prop="currentContent"
          label="正确内容">
      </el-table-column>
      <el-table-column
          label="操作">
        <template slot-scope="scope">
          <el-button
              size="mini"
              @click="handleEdit(scope.row)">编辑
          </el-button>
            <el-dialog title="添加题目" :visible.sync="updateVisible" width="50%">
              <el-form ref="questionForm" :model="updateGameOption" label-width="120px">
                <el-form-item label="题目内容">
                  <el-input v-model="updateGameOption.gameOption.content"></el-input>
                </el-form-item>
                <el-form-item label="选项">
                  <el-row v-for="(option,index) in updateGameOption.optionItems" key="index">
                    选项{{index+1}}:<el-input type="text" :value="option.content"/>
                  </el-row>
                </el-form-item>
              </el-form>
              <el-button @click="dialogVisible = false">取 消</el-button>
              <el-button type="primary" @click="update(updateGameOption)">确 定</el-button>
            </el-dialog>


          <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import axios from "axios";
export default {
  data() {
    return {
      questions: [],
      dialogVisible: false,
      updateVisible: false,
      currentQuestion: {},
      gameOption:{
        gameOption: {
          content:"",
          videoPath:"",
          currentPath: ""
        },
        optionItems:[
          {
            content:"",
            videoPath:""
          }
        ]
      },
      updateGameOption:{
        gameOption:{
          content:"",
          videoPath:"",
          currentPath:""
        },
        optionItems:[
          {
            content:"",
            videoPath:""
          }
        ]
      },fileList:[]
    };
  },
  methods: {
    handleEdit(row) {
      console.log(row)
      this.updateGameOption.gameOption.content = row.content
      this.updateGameOption.gameOption.videoPath = row.videoPath
      this.updateVisible = true;
      axios({
        url:"http://localhost:8089/wx/game/get?gameId="+row.id,
        methods:"get"
      }).then((res=>{
        this.updateGameOption.optionItems=res.data
      }))
    },
    handleDelete(index, row) {
      console.log(row)
    },
    saveEdit() {
      // 这里可以提交编辑后的题目数据到后端
      this.dialogVisible = false;
    },
    queryQuestion(){
      axios({
        url:"http://localhost:8089/wx/game/query",
        method:"get"
      }).then((res)=>{
        console.log(res.data)
        this.questions=res.data
      })
    },
    add(question){
      console.log(question)
      axios({
        url: "http://localhost:8089/wx/game/add",
        method: "post",
        header: {
          "Content-type":"application/json;charset=utf-8"
        },data: question
      }).then(res=>{
        console.log(res.data)
      })
      this.dialogVisible=true
    },
    update(value){
      console.log(value)
      axios({
        url:"http://localhost:8089/wx/game/update",
        method:"post",
        data: value,
        header:{
          "Content-type":"application/json;charset=utf-8"
        }
      }).then((res)=>{
        console.log(res)
      })
    },
    openDialog() {
      this.dialogVisible = true;
    },
    addOption() {
      this.gameOption.optionsItem.push({ content: '', videoPath: [] });
    },
    updateOption(){
      this.updateGameOption.optionsItem.push({ content: '', videoPath: [] });
    },
    removeOption() {
      this.gameOption.optionsItem.pop();
    },
    removeUpdateOption(){
      this.updateGameOption.optionsItem.pop();
    },
    submitOptionUpload(index) {
      console.log(index)
      // 确认上传选项文件的操作
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // 提交表单的操作
          this.dialogVisible = false;
        } else {
          return false;
        }
      });
    },
    uploadFile(event){
      this.fileList=event.target.files[0]
      console.log(this.fileList)
      this.gameOption.gameOption.videoPath=this.fileList
    },
    handleChange(value){
      console.log(value)
    }

  },
  mounted() {
    this.queryQuestion();
  }
};
</script>
